(function(app){
	app
		.controller("content_38_inputCtrl",["$scope","myFactory",'$http','$stateParams','$state',"$q",'$timeout','$location','$ionicHistory',function($scope,myFactory,$http,$stateParams,$state,$q,$timeout,$location,$ionicHistory){
			//接收参数
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var searchParam = $stateParams.searchParam;
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item038
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			//参数绑定
			$scope.chdrnum01 =  $stateParams.chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			$scope.loanType = "1"//还款方式，默认为第一种
			var token=sessionStorage.getItem("token");
/**********************************************************初始化部分begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.item38 = itemData.item38;
						
						//处理日期显示格式 20170129---->2017-01-28
						for(var i=0 ; i<$scope.item38.loanList.length ; i++){
							$scope.item38.loanList[i].loanstdate = String($scope.item38.loanList[i].loanstdate);
							if($.trim($scope.item38.loanList[i])!=""){
								$scope.item38.loanList[i].loanstdate = $scope.item38.loanList[i].loanstdate.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");
							}
						}
						
						//初始默认选择第一种方式
						$scope.changeLoanType(1);
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
			
			

/**********************************************************初始化部分end******************************************************/
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回函数
			$scope.goBack = function () {
				myFactory.goBack();
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}
			//保单详情弹窗
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn38Input','policyWindowOn38Input','policyCloseXOn38Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn38Input = function(){
				modal('close','policyModalOn38Input','policyWindowOn38Input','policyCloseXOn38Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//客户须知
			$scope.agrement = function(){
				
				$("#myDialog1").fadeToggle(50);    //显示弹框

				$("#myDialog1 .myDialog_content .btn_close").click(function(){
					$(this).parents('#myDialog1').fadeOut(50);    //绑定确定按键部分
				});
				
				$("#myDialog1 .myDialog_black").click(function(){  //即点击遮罩也隐藏
					$(this).parent('#myDialog1').fadeOut(50);
				});
			}
			//查看借款详情
			$scope.detail = function(){
				$("#myDialog").fadeToggle(50);

				$("#myDialog .myDialog_content .btn_close").click(function(){
					$(this).parents('#myDialog').fadeOut(50);
				});
				
				$("#myDialog .myDialog_black").click(function(){
					$(this).parent('#myDialog').fadeOut(50);
				});
			}
			//改变还款类型
			$scope.changeLoanType = function(type){
				if(type=="1"){
					$('#bamount').val($scope.item38.amount);
					$('#bamount').attr("readonly",true);
//					$scope.loanType = "1" ;
				}else if(type=="2"){
					$('#bamount').val('');
					$('#bamount').attr("readonly",false);
//					$scope.loanType = "2" ;
				}
			}
	
			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				//检测查阅弹框
				if($scope.readFlag == false){
					myFactory.alert("","请阅读《申请须知》","info");
					return
				}
				
			
				//对输入金额进行校验
			    var bamount = $("#bamount").val();
		        if(parseFloat(bamount)== 0 || bamount==""){
		        	myFactory.alert("","请填写还款金额!","info");
					return ;
		        }else{
		        	var reg = /^\d{0,8}\.{0,1}(\d{1,2})?$/;
		        	if(!reg.test(bamount)){
		        		myFactory.alert("","请填写正确还款金额!","info");
		        		return;
		        	}
		        }
		        bamount = parseFloat(bamount);
		        var amount = parseFloat($("#amount").val());
		        console.log("amount");
		        if(bamount>amount){
		        	myFactory.alert("","还款金额大于借款金额，需重新输入!","info");
					return;
		        }
				
				var item={
					"itemId":itemId,
					"amount" :amount,
					"bamount":bamount
				}
				
				$ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
				
				
			}
					
		}]);
		//指针弹窗，不同APP，要提取到app.controller("content_36_inputCtrl")后面
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)